{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyM6XIXWkNS/g6krr9fp/fYP",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/LC1332/Chat-Haruhi-Suzumiya/blob/main/notebook/%E5%AF%B9%E5%A3%B0%E7%BA%B9%E7%9A%84%E4%BA%A4%E5%8F%89%E6%A3%80%E9%AA%8C.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# 对凉宫春日片段声纹识别的交叉验证\n",
        "\n",
        "**Chat凉宫春日**是模仿凉宫春日等一系列动漫人物，使用近似语气、个性和剧情聊天的语言模型，\n",
        "\n",
        "<details>\n",
        "  <summary> 本项目由李鲁鲁，冷子昂，闫晨曦，封小洋，scixing，贾曜恺等开发。 </summary>\n",
        "\n",
        "李鲁鲁发起了项目，并完成了最早的版本，在多个微信群实现了测试。完成了GPT自动生成对话部分。\n",
        "\n",
        "冷子昂参与了早期Gradio的开发，并且参与了后端和前端的选型。debug和最终上线了app.py\n",
        "\n",
        "闫晨曦将李鲁鲁的notebook重构为app.py\n",
        "\n",
        "封小洋进行了中文转日文模型的选型，完成了针对台词抽取图片的工具，（将要完成）和haruhi图片分类器\n",
        "\n",
        "scixing实践了VITS语音，完成了台词对应的语音抽取，（将要完成）特定人物的语音分类器。\n",
        "\n",
        "贾曜恺开发了一个前端实现方案，待测试。\n",
        "\n",
        "</details>\n",
        "\n",
        "本脚本由李鲁鲁开发，用于验证声纹特征交叉验证的准确率"
      ],
      "metadata": {
        "id": "GGXPYbbE6tNT"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "- [x] 整理之前的GPTData文件夹\n",
        "- [x] 确认fxy和scixing是否做了这个事情\n",
        "- [x] 转移声纹和声音文件夹到GoogleDrive\n",
        "- [x] 解析标注文件夹，将特征整理成feature，label形式\n",
        "- [x] 进行交叉验证\n",
        "- [x] 给出自动阈值\n",
        "- [x] 进行分类尝试"
      ],
      "metadata": {
        "id": "oAGW8Idq7hsN"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AEG438Mu6koY",
        "outputId": "c4902d39-549c-4e6f-92a7-746a9fb68859"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "#mount google drive to colab\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!ls /content/drive/MyDrive/GPTData/HaruhiRoleVoices"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7YtoXbTsdq3W",
        "outputId": "9794e278-49b9-4df6-b47c-af4b5f04fdd6"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "'Haruhi ep10.zip'  'Haruhi ep1.zip'   voiceWithCha.zip\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#copy all zip to /content\n",
        "!cp /content/drive/MyDrive/GPTData/HaruhiRoleVoices/*.zip /content/\n",
        "\n"
      ],
      "metadata": {
        "id": "Tuy81xuGd7Qv"
      },
      "execution_count": 26,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#unzip all zip file\n",
        "!unzip -q \"/content/Haruhi ep10.zip\"\n",
        "!unzip -q \"/content/Haruhi ep1.zip\"\n",
        "# !unzip \"/content/voiceWithCha.zip\""
      ],
      "metadata": {
        "id": "S43MGJ3TeGds"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#unzip /content/voiceWithCha.zip into /content/roles quitely\n",
        "!unzip -q /content/voiceWithCha.zip -d /content/roles"
      ],
      "metadata": {
        "id": "Ew62SQLbeVTt"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# scan subfolder in /content/roles and save into a list\n",
        "import os\n",
        "roles = os.listdir('/content/roles')\n",
        "\n",
        "# for each role in roles, scan /content/roles/[role] and save into a list of list\n",
        "roles_list = []\n",
        "roles_list_full = []\n",
        "for role in roles:\n",
        "\n",
        "    sub_list = os.listdir(os.path.join('/content/roles',role))\n",
        "    roles_list.append(sub_list)\n",
        "\n",
        "    full_name_list = ['/content/roles/'+role+'/'+file for file in sub_list]\n",
        "    roles_list_full.append(full_name_list)\n"
      ],
      "metadata": {
        "id": "CnwcSoNtfOK0"
      },
      "execution_count": 16,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(roles_list[0])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qAhDe0ZJfZqy",
        "outputId": "a0531d06-ac2a-4ef1-ab85-d66eaa481774"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "['68_而是依照凉宫同学的思维模式进行思考而得出的结论.wav', '55_就需要到和那些事件相应的场所去才行.wav', '51_但是这里说那些现实主义的话就太煞风景了.wav', '63_换言之  名侦探所到之处必有事件发生.wav', '54_想要遭遇这种非现实的神秘事件.wav', '44_推理故事作品里的名侦探们.wav', '58_的确  现实世界是不会像故事那样的.wav', '31_我有个非常有钱的远房亲戚.wav', '32_他的钱多到可以买下一个无人岛并且在上面盖上别墅.wav', '50_这样的事件只存在于虚构而非现实的故事世界中.wav', '43_那么你想想「名侦探」那三个字吧.wav', '56_因为创作出来的名侦探们都是这样被卷进事件中的.wav', '349_新川  有什么事吗？.wav', '59_但是现在我们所要去的「孤岛」这种地方.wav', '60_一般人都认为是最适合成为杀人事件舞台的地方.wav', '193_是除我们之外应邀前来的客人.wav', '69_也就是说呢  她想尝试一次看看.wav', '37_臣拜领此职.wav', '52_因为凉宫同学似乎就是想要投身于这样的虚构世界中.wav', '67_「名侦探」和「Closed Circle」并不是我所空想出来的.wav', '64_被称为名侦探的人都有能够呼唤事件的超自然能力.wav', '346_怪不得凉宫同学吃过早饭后.wav', '45_不知道为什么会不停地卷入难以理解的事件中.wav', '33_那栋别墅前几天刚刚落成.wav', '66_我有时候也会偶尔认真一回的.wav']\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "candidate_path = [\n",
        "    \"/content/Haruhi ep1\",\n",
        "    \"/content/Haruhi ep10\"\n",
        "]"
      ],
      "metadata": {
        "id": "KNUPT9x3hdxI"
      },
      "execution_count": 21,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import pickle\n",
        "import numpy as np\n",
        "\n",
        "\n",
        "features = []\n",
        "labels = []\n",
        "dim = 0\n",
        "\n",
        "count = 0\n",
        "\n",
        "for role in roles:\n",
        "    print(role,end=' ')\n",
        "\n",
        "    for file in roles_list[roles.index(role)]:\n",
        "        # print(file)\n",
        "\n",
        "        deal_flag = False\n",
        "\n",
        "        for candidate in candidate_path:\n",
        "\n",
        "            candidate_fname = os.path.join(candidate,'voice',file)\n",
        "\n",
        "            if os.path.exists(candidate_fname):\n",
        "                # print(candidate_fname,'found')\n",
        "                deal_flag = True\n",
        "                feature_fname = os.path.join(candidate,'feature',file) + '.pkl'\n",
        "                break\n",
        "\n",
        "        if deal_flag == False:\n",
        "            print('warning!',file,'not found')\n",
        "            continue\n",
        "\n",
        "        if not os.path.exists(feature_fname):\n",
        "            print('warning!',feature_fname,'not found')\n",
        "            continue\n",
        "\n",
        "        # pinkle load feature_fname\n",
        "        with open(feature_fname,'rb') as f:\n",
        "            feature = pickle.load(f)\n",
        "\n",
        "        count += 1\n",
        "\n",
        "        # append numpy array feature into numpy matrix features\n",
        "        if dim == 0:\n",
        "            features = feature\n",
        "            dim = feature.shape[0]\n",
        "            # print(dim)\n",
        "        else:\n",
        "            features = np.vstack((features,feature))\n",
        "\n",
        "        labels.append(role)\n",
        "\n",
        "        # print(feature_fname,'found')\n",
        "\n",
        "\n",
        "\n",
        "    # break\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GJXvx82AfaQa",
        "outputId": "73c19333-f71a-4a23-bffe-19ab5061fd49"
      },
      "execution_count": 45,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "古泉 多丸裕 谷口 新川先生 阿虚妹妹 长门 春日 朝仓 阿虚 森小姐 忘了叫啥的角色 多丸圭一 朝比奈 "
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "feature是一个N*D的numpy矩阵，每行存储了一个D维特征\n",
        "labels是一个python的list of string，表示每行对应的数据的标签。\n",
        "我想验证这批数据使用K近邻分类，在10折交叉时的准确率，请用python为我实现。"
      ],
      "metadata": {
        "id": "wLeCL3mzlnft"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "from sklearn.model_selection import cross_val_score\n",
        "\n",
        "# 创建 K 近邻分类器模型\n",
        "k = 1  # 假设选择 k 值为 3\n",
        "knn = KNeighborsClassifier(n_neighbors=k, metric='cosine')\n",
        "\n",
        "# 将特征转换为 NumPy 数组\n",
        "features = np.array(features)\n",
        "\n",
        "# 将标签转换为 NumPy 数组\n",
        "labels = np.array(labels)\n",
        "\n",
        "# 计算 10 折交叉验证的准确率\n",
        "cv_accuracy = cross_val_score(knn, features, labels, cv=5)\n",
        "\n",
        "# 打印每折交叉验证的准确率\n",
        "for fold, accuracy in enumerate(cv_accuracy, 1):\n",
        "    print(f\"Fold {fold}: {accuracy}\")\n",
        "\n",
        "# 打印平均准确率\n",
        "mean_accuracy = np.mean(cv_accuracy)\n",
        "print(f\"Average Accuracy: {mean_accuracy}\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9GmzU9llljm0",
        "outputId": "c8055c39-6c34-42c5-bc0d-eef6a4beddd3"
      },
      "execution_count": 50,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Fold 1: 0.8809523809523809\n",
            "Fold 2: 0.9047619047619048\n",
            "Fold 3: 0.8780487804878049\n",
            "Fold 4: 0.8536585365853658\n",
            "Fold 5: 0.8536585365853658\n",
            "Average Accuracy: 0.8742160278745643\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/sklearn/model_selection/_split.py:700: UserWarning: The least populated class in y has only 2 members, which is less than n_splits=5.\n",
            "  warnings.warn(\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "定义一个函数，给定roles和roles_list，输出feature和label"
      ],
      "metadata": {
        "id": "hkUs27CmtMI3"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 定义gather_feature_label\n",
        "\n",
        "def gather_feature_label( roles, roles_list ):\n",
        "    features = []\n",
        "    labels = []\n",
        "    dim = 0\n",
        "\n",
        "    count = 0\n",
        "\n",
        "    for role in roles:\n",
        "        print(role,end=' ')\n",
        "\n",
        "        for file in roles_list[roles.index(role)]:\n",
        "            # print(file)\n",
        "\n",
        "            deal_flag = False\n",
        "\n",
        "            for candidate in candidate_path:\n",
        "\n",
        "                candidate_fname = os.path.join(candidate,'voice',file)\n",
        "\n",
        "                if os.path.exists(candidate_fname):\n",
        "                    # print(candidate_fname,'found')\n",
        "                    deal_flag = True\n",
        "                    feature_fname = os.path.join(candidate,'feature',file) + '.pkl'\n",
        "                    break\n",
        "\n",
        "            if deal_flag == False:\n",
        "                print('warning!',file,'not found')\n",
        "                continue\n",
        "\n",
        "            if not os.path.exists(feature_fname):\n",
        "                print('warning!',feature_fname,'not found')\n",
        "                continue\n",
        "\n",
        "            # pinkle load feature_fname\n",
        "            with open(feature_fname,'rb') as f:\n",
        "                feature = pickle.load(f)\n",
        "\n",
        "            count += 1\n",
        "\n",
        "            # append numpy array feature into numpy matrix features\n",
        "            if dim == 0:\n",
        "                features = feature\n",
        "                dim = feature.shape[0]\n",
        "                # print(dim)\n",
        "            else:\n",
        "                features = np.vstack((features,feature))\n",
        "\n",
        "            labels.append(role)\n",
        "\n",
        "    return features, labels\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "cellView": "form",
        "id": "mvilaBuxjuz1"
      },
      "execution_count": 51,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import random\n",
        "\n",
        "roles_sel = []\n",
        "roles_list_sel = []\n",
        "\n",
        "M = 8\n",
        "\n",
        "for role in roles:\n",
        "    wav_list = roles_list[roles.index(role)]\n",
        "\n",
        "    # if len(wav_list) < M:\n",
        "        # continue\n",
        "\n",
        "    # random pick 5 element from wav_list\n",
        "    random.shuffle(wav_list)\n",
        "    # wav_list = wav_list[:]\n",
        "\n",
        "    roles_sel.append(role)\n",
        "    roles_list_sel.append(wav_list)"
      ],
      "metadata": {
        "id": "R14RbVJ-waJw"
      },
      "execution_count": 95,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "feat_sel, label_sel = gather_feature_label(roles_sel,roles_list_sel)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NG2Rzp8Zw6jI",
        "outputId": "54ed77e0-467b-46e3-80d4-d5f338af173c"
      },
      "execution_count": 96,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "古泉 多丸裕 谷口 新川先生 阿虚妹妹 长门 春日 朝仓 阿虚 森小姐 忘了叫啥的角色 多丸圭一 朝比奈 "
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "feature是一个N*D的numpy矩阵，每行存储了一个D维特征\n",
        "labels是一个python的list of string，表示每行对应的数据的标签。\n",
        "\n",
        "实现一个Python类My_Classifier,\n",
        "这个类可以使用feature和labels初始化一个 sklearn的1-NN的最近邻分类器，使用cosine度量\n",
        "这个类有一个predict方法，输出一个tuple，为(类别，距离)"
      ],
      "metadata": {
        "id": "_c2fPj2CxS4O"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "from scipy.spatial.distance import cosine\n",
        "\n",
        "class My_Classifier:\n",
        "    def __init__(self, feature, labels):\n",
        "        self.feature = feature\n",
        "        self.labels = labels\n",
        "\n",
        "    def predict(self, x):\n",
        "        min_dist = float('inf')\n",
        "        predicted_label = None\n",
        "\n",
        "        for i, f in enumerate(self.feature):\n",
        "            dist = cosine(x, f)\n",
        "            if dist < min_dist:\n",
        "                min_dist = dist\n",
        "                predicted_label = self.labels[i]\n",
        "\n",
        "        return predicted_label, min_dist"
      ],
      "metadata": {
        "id": "LUDEu-kRxD3B"
      },
      "execution_count": 97,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "my_classifier = My_Classifier(feat_sel, label_sel)\n",
        "\n",
        "corrent_dists = []\n",
        "wrong_dists = []\n",
        "\n",
        "for i in range(len(labels)):\n",
        "    # read i-th row from features, save as feat\n",
        "    feat = features[i,:]\n",
        "    # read i-th row from labels, save as label\n",
        "    label = labels[i]\n",
        "\n",
        "    # predict label of i-th row\n",
        "    predicted_label, distance = my_classifier.predict(feat)\n",
        "\n",
        "    # if distance < 1e-3:\n",
        "    #     continue\n",
        "\n",
        "    if label == predicted_label:\n",
        "        corrent_dists.append(distance)\n",
        "    else:\n",
        "        wrong_dists.append(distance)\n"
      ],
      "metadata": {
        "id": "DDZCt9qzxHO5"
      },
      "execution_count": 98,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "corrent_dists 和 wrong_dists 都是python的list of float, 统计两者的均值和方差，用plotly将两个高斯分布进行可视化，使用不同的颜色标示两个分布。"
      ],
      "metadata": {
        "id": "HVBzV3cizGg9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#@title 高斯可视化\n",
        "\n",
        "import numpy as np\n",
        "import plotly.graph_objects as go\n",
        "\n",
        "# 计算均值和方差\n",
        "corrent_mean = np.mean(corrent_dists)\n",
        "corrent_var = np.var(corrent_dists)\n",
        "wrong_mean = np.mean(wrong_dists)\n",
        "wrong_var = np.var(wrong_dists)\n",
        "\n",
        "# 可视化高斯分布\n",
        "x = np.linspace(min(corrent_dists + wrong_dists), max(corrent_dists + wrong_dists), 100)\n",
        "corrent_distribution = (1 / np.sqrt(2 * np.pi * corrent_var)) * np.exp(-0.5 * ((x - corrent_mean) / np.sqrt(corrent_var)) ** 2)\n",
        "wrong_distribution = (1 / np.sqrt(2 * np.pi * wrong_var)) * np.exp(-0.5 * ((x - wrong_mean) / np.sqrt(wrong_var)) ** 2)\n",
        "\n",
        "fig = go.Figure()\n",
        "fig.add_trace(go.Scatter(x=x, y=corrent_distribution, mode='lines', name='Correct Distribution'))\n",
        "fig.add_trace(go.Scatter(x=x, y=wrong_distribution, mode='lines', name='Wrong Distribution'))\n",
        "\n",
        "# 添加布局和标题\n",
        "fig.update_layout(\n",
        "    title='Gaussian Distributions',\n",
        "    xaxis_title='Distance',\n",
        "    yaxis_title='Probability Density',\n",
        ")\n",
        "\n",
        "# 显示图表\n",
        "fig.show()\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 542
        },
        "id": "H_MGTbL8ycZk",
        "outputId": "a0bef945-7a5d-4039-c513-9676b9e66e80"
      },
      "execution_count": 72,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "<html>\n",
              "<head><meta charset=\"utf-8\" /></head>\n",
              "<body>\n",
              "    <div>            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_SVG\"></script><script type=\"text/javascript\">if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
              "        <script src=\"https://cdn.plot.ly/plotly-2.18.2.min.js\"></script>                <div id=\"bdc3b793-3b5d-4740-ae54-04e5cbedb0a4\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div>            <script type=\"text/javascript\">                                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById(\"bdc3b793-3b5d-4740-ae54-04e5cbedb0a4\")) {                    Plotly.newPlot(                        \"bdc3b793-3b5d-4740-ae54-04e5cbedb0a4\",                        [{\"mode\":\"lines\",\"name\":\"Correct Distribution\",\"x\":[0.0,0.008602993355857002,0.017205986711714003,0.025808980067571007,0.034411973423428006,0.043014966779285006,0.05161796013514201,0.06022095349099901,0.06882394684685601,0.07742694020271301,0.08602993355857001,0.09463292691442701,0.10323592027028403,0.11183891362614103,0.12044190698199803,0.129044900337855,0.13764789369371203,0.14625088704956904,0.15485388040542603,0.16345687376128304,0.17205986711714003,0.18066286047299704,0.18926585382885402,0.19786884718471104,0.20647184054056805,0.21507483389642504,0.22367782725228205,0.23228082060813904,0.24088381396399605,0.24948680731985304,0.25808980067571,0.26669279403156704,0.27529578738742405,0.28389878074328107,0.2925017740991381,0.30110476745499504,0.30970776081085205,0.31831075416670906,0.3269137475225661,0.33551674087842304,0.34411973423428005,0.35272272759013706,0.3613257209459941,0.3699287143018511,0.37853170765770805,0.38713470101356506,0.3957376943694221,0.4043406877252791,0.4129436810811361,0.42154667443699306,0.4301496677928501,0.4387526611487071,0.4473556545045641,0.45595864786042106,0.4645616412162781,0.4731646345721351,0.4817676279279921,0.4903706212838491,0.4989736146397061,0.5075766079955631,0.51617960135142,0.5247825947072771,0.5333855880631341,0.5419885814189911,0.5505915747748481,0.5591945681307051,0.5677975614865621,0.5764005548424191,0.5850035481982762,0.5936065415541331,0.6022095349099901,0.6108125282658471,0.6194155216217041,0.6280185149775611,0.6366215083334181,0.6452245016892751,0.6538274950451322,0.6624304884009892,0.6710334817568461,0.6796364751127031,0.6882394684685601,0.6968424618244171,0.7054454551802741,0.7140484485361311,0.7226514418919882,0.7312544352478452,0.7398574286037022,0.7484604219595591,0.7570634153154161,0.7656664086712731,0.7742694020271301,0.7828723953829871,0.7914753887388442,0.8000783820947012,0.8086813754505582,0.8172843688064152,0.8258873621622722,0.8344903555181291,0.8430933488739861,0.8516963422298431],\"y\":[1.193817758650189,1.2483514427186477,1.3028467979821754,1.3570863499394294,1.4108448733198733,1.463890850502762,1.515988051273372,1.5668972200117937,1.6163778543904264,1.6641900578123845,1.7100964461933004,1.7538640883098409,1.7952664578420985,1.8340853744519459,1.8701129107881411,1.9031532422082944,1.9330244162681154,1.9595600196532448,1.982610721214943,2.0020456711073784,2.0177537376936177,2.029644565865288,2.037649442676551,2.0417219586900144,2.0418384561290193,2.0379982577816023,2.0302236735574315,2.0185597846088976,2.003074007938953,1.9838554473789134,1.961014039677848,1.9346795071523784,1.9050001308556315,1.8721413604950308,1.8362842793237253,1.7976239439187092,1.7563676201152933,1.712732937374548,1.6669459845066177,1.6192393699545564,1.5698502697636458,1.51901848593011,1.4669845370570986,1.4139878021672612,1.3602647371578314,1.306047181768202,1.2515607730975813,1.197023479700327,1.1426442681399784,1.0886219116416573,1.035143948188544,0.9823857931028283,0.9305100088745817,0.8796657327908598,0.8299882608063048,0.7815987841164916,0.7346042730729012,0.6890975014362354,0.6451572025204664,0.6028483475467078,0.5622225355119868,0.5233184830870566,0.48616260248883286,0.45076965492205834,0.41714346704289534,0.38527769795240147,0.3551566444655463,0.3267560728045001,0.3000440654144309,0.27498187227574716,0.25152475686759523,0.2296228278021019,0.20922184807613328,0.19026401485662933,0.17268870370714057,0.15643317215863914,0.1414332185101199,0.12762379269876203,0.11493955699226714,0.10331539511610788,0.09268686922660871,0.0829906248697685,0.07416474472032918,0.06614905247238337,0.05888536875024724,0.05231772132639166,0.046392512273424086,0.04105864494216552,0.03626761385160808,0.03197356070365156,0.0281332998014095,0.02470631616043186,0.021654739563649716,0.018943297729591774,0.01653925164587802,0.01441231597245586,0.012534567247597673,0.010880342440084073,0.009426130188627833,0.008150456859372933],\"type\":\"scatter\"},{\"mode\":\"lines\",\"name\":\"Wrong Distribution\",\"x\":[0.0,0.008602993355857002,0.017205986711714003,0.025808980067571007,0.034411973423428006,0.043014966779285006,0.05161796013514201,0.06022095349099901,0.06882394684685601,0.07742694020271301,0.08602993355857001,0.09463292691442701,0.10323592027028403,0.11183891362614103,0.12044190698199803,0.129044900337855,0.13764789369371203,0.14625088704956904,0.15485388040542603,0.16345687376128304,0.17205986711714003,0.18066286047299704,0.18926585382885402,0.19786884718471104,0.20647184054056805,0.21507483389642504,0.22367782725228205,0.23228082060813904,0.24088381396399605,0.24948680731985304,0.25808980067571,0.26669279403156704,0.27529578738742405,0.28389878074328107,0.2925017740991381,0.30110476745499504,0.30970776081085205,0.31831075416670906,0.3269137475225661,0.33551674087842304,0.34411973423428005,0.35272272759013706,0.3613257209459941,0.3699287143018511,0.37853170765770805,0.38713470101356506,0.3957376943694221,0.4043406877252791,0.4129436810811361,0.42154667443699306,0.4301496677928501,0.4387526611487071,0.4473556545045641,0.45595864786042106,0.4645616412162781,0.4731646345721351,0.4817676279279921,0.4903706212838491,0.4989736146397061,0.5075766079955631,0.51617960135142,0.5247825947072771,0.5333855880631341,0.5419885814189911,0.5505915747748481,0.5591945681307051,0.5677975614865621,0.5764005548424191,0.5850035481982762,0.5936065415541331,0.6022095349099901,0.6108125282658471,0.6194155216217041,0.6280185149775611,0.6366215083334181,0.6452245016892751,0.6538274950451322,0.6624304884009892,0.6710334817568461,0.6796364751127031,0.6882394684685601,0.6968424618244171,0.7054454551802741,0.7140484485361311,0.7226514418919882,0.7312544352478452,0.7398574286037022,0.7484604219595591,0.7570634153154161,0.7656664086712731,0.7742694020271301,0.7828723953829871,0.7914753887388442,0.8000783820947012,0.8086813754505582,0.8172843688064152,0.8258873621622722,0.8344903555181291,0.8430933488739861,0.8516963422298431],\"y\":[0.00016657420747907353,0.0002232110641274561,0.00029778446339318574,0.00039551841375833605,0.000523009689054019,0.0006885430780189564,0.0009024660098593636,0.0011776302308995753,0.0015299082726284322,0.0019787922239646156,0.0025480817192817342,0.003266666991293397,0.004169411223603828,0.005298134181551044,0.006702696117696417,0.008442177167866416,0.010586142822470111,0.013215980551159728,0.016426286288780668,0.020326272314867135,0.025041160190312058,0.030713514028515732,0.037504460718334305,0.04559473509899788,0.05518547990207798,0.06649872298062114,0.07977744845878819,0.09528517452047623,0.1133049492017235,0.13413767734804502,0.1580996974013264,0.18551953638092106,0.21673378570276164,0.25208205957384844,0.2919010216554855,0.33651749433182915,0.3862406978219605,0.4413537028310284,0.5021042194636085,0.5686948854582755,0.6412732569306944,0.7199217430041023,0.8046477600769582,0.8953744100644468,0.9919320078012412,1.09405079406785,1.2013551707832975,1.3133597825041867,1.429467742629277,1.5489712632992307,1.6710548951693744,1.794801517928038,1.9192011462014242,2.043162530533442,2.165527442243617,2.2850874374252292,2.4006028027894732,2.5108232983360996,2.6145102328121514,2.7104593413208673,2.7975238836195633,2.874637349414537,2.940835145432468,2.9952746494910754,3.037253049539936,3.0662224400448856,3.0818017225136054,3.083784948846889,3.072145852169584,3.04703842579745,3.0087935325002158,2.957911648430869,2.8950519641765204,2.8210181747158574,2.736741386431535,2.643260649130638,2.5417016814617215,2.433254397273041,2.3191498573944624,2.200637266095294,2.0789616050768416,1.9553424521796066,1.830954469647524,1.706909971013333,1.5842438900795244,1.463901383916236,1.346728208155175,1.2334639108443615,1.124737804139563,1.0210675940861564,0.9228604800720341,0.8304164789646132,0.7439336855829636,0.663515151449369,0.5891770475498685,0.5208577733993869,0.4584276828811444,0.4016991155782301,0.3504364488509027,0.3043659188059722],\"type\":\"scatter\"}],                        {\"template\":{\"data\":{\"histogram2dcontour\":[{\"type\":\"histogram2dcontour\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"choropleth\":[{\"type\":\"choropleth\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"histogram2d\":[{\"type\":\"histogram2d\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"heatmap\":[{\"type\":\"heatmap\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"heatmapgl\":[{\"type\":\"heatmapgl\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"contourcarpet\":[{\"type\":\"contourcarpet\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"contour\":[{\"type\":\"contour\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"surface\":[{\"type\":\"surface\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"mesh3d\":[{\"type\":\"mesh3d\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"parcoords\":[{\"type\":\"parcoords\",\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterpolargl\":[{\"type\":\"scatterpolargl\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"scattergeo\":[{\"type\":\"scattergeo\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterpolar\":[{\"type\":\"scatterpolar\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"scattergl\":[{\"type\":\"scattergl\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatter3d\":[{\"type\":\"scatter3d\",\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scattermapbox\":[{\"type\":\"scattermapbox\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterternary\":[{\"type\":\"scatterternary\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scattercarpet\":[{\"type\":\"scattercarpet\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"baxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"type\":\"carpet\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"#EBF0F8\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"#C8D4E3\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}],\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}]},\"layout\":{\"autotypenumbers\":\"strict\",\"colorway\":[\"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#FFA15A\",\"#19d3f3\",\"#FF6692\",\"#B6E880\",\"#FF97FF\",\"#FECB52\"],\"font\":{\"color\":\"#2a3f5f\"},\"hovermode\":\"closest\",\"hoverlabel\":{\"align\":\"left\"},\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"#E5ECF6\",\"polar\":{\"bgcolor\":\"#E5ECF6\",\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"ternary\":{\"bgcolor\":\"#E5ECF6\",\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"colorscale\":{\"sequential\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"sequentialminus\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"diverging\":[[0,\"#8e0152\"],[0.1,\"#c51b7d\"],[0.2,\"#de77ae\"],[0.3,\"#f1b6da\"],[0.4,\"#fde0ef\"],[0.5,\"#f7f7f7\"],[0.6,\"#e6f5d0\"],[0.7,\"#b8e186\"],[0.8,\"#7fbc41\"],[0.9,\"#4d9221\"],[1,\"#276419\"]]},\"xaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"automargin\":true,\"zerolinewidth\":2},\"yaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"automargin\":true,\"zerolinewidth\":2},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\",\"gridwidth\":2},\"yaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\",\"gridwidth\":2},\"zaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\",\"gridwidth\":2}},\"shapedefaults\":{\"line\":{\"color\":\"#2a3f5f\"}},\"annotationdefaults\":{\"arrowcolor\":\"#2a3f5f\",\"arrowhead\":0,\"arrowwidth\":1},\"geo\":{\"bgcolor\":\"white\",\"landcolor\":\"#E5ECF6\",\"subunitcolor\":\"white\",\"showland\":true,\"showlakes\":true,\"lakecolor\":\"white\"},\"title\":{\"x\":0.05},\"mapbox\":{\"style\":\"light\"}}},\"title\":{\"text\":\"Gaussian Distributions\"},\"xaxis\":{\"title\":{\"text\":\"Distance\"}},\"yaxis\":{\"title\":{\"text\":\"Probability Density\"}}},                        {\"responsive\": true}                    ).then(function(){\n",
              "                            \n",
              "var gd = document.getElementById('bdc3b793-3b5d-4740-ae54-04e5cbedb0a4');\n",
              "var x = new MutationObserver(function (mutations, observer) {{\n",
              "        var display = window.getComputedStyle(gd).display;\n",
              "        if (!display || display === 'none') {{\n",
              "            console.log([gd, 'removed!']);\n",
              "            Plotly.purge(gd);\n",
              "            observer.disconnect();\n",
              "        }}\n",
              "}});\n",
              "\n",
              "// Listen for the removal of the full notebook cells\n",
              "var notebookContainer = gd.closest('#notebook-container');\n",
              "if (notebookContainer) {{\n",
              "    x.observe(notebookContainer, {childList: true});\n",
              "}}\n",
              "\n",
              "// Listen for the clearing of the current output cell\n",
              "var outputEl = gd.closest('.output');\n",
              "if (outputEl) {{\n",
              "    x.observe(outputEl, {childList: true});\n",
              "}}\n",
              "\n",
              "                        })                };                            </script>        </div>\n",
              "</body>\n",
              "</html>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "corrent_dists 和 wrong_dists 都是python的list of float, 统计两者的均值和方差。\n",
        "\n",
        "然后计算 P( correct | dist )函数，即\n",
        "\n",
        "P( correct | dist ) = P( dist | corret ) / (P( dist | corret ) + P( dist | wrong ) )\n",
        "\n",
        "用plotly进行可视化"
      ],
      "metadata": {
        "id": "Q7SEBvx-z_Kc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "\n",
        "corrent_mean = np.mean(corrent_dists)\n",
        "corrent_var = np.var(corrent_dists)\n",
        "wrong_mean = np.mean(wrong_dists)\n",
        "wrong_var = np.var(wrong_dists)\n",
        "\n",
        "def gaussian_pdf(x, mean, var):\n",
        "    return (1 / np.sqrt(2 * np.pi * var)) * np.exp(-0.5 * ((x - mean) / np.sqrt(var)) ** 2)\n",
        "\n",
        "x = np.linspace(min(corrent_dists + wrong_dists), max(corrent_dists + wrong_dists), 100)\n",
        "p_dist_corrent = gaussian_pdf(x, corrent_mean, corrent_var)\n",
        "p_dist_wrong = gaussian_pdf(x, wrong_mean, wrong_var)\n",
        "\n",
        "import plotly.graph_objects as go\n",
        "\n",
        "p_correct_given_dist = p_dist_corrent / (p_dist_corrent + p_dist_wrong)\n",
        "\n",
        "fig = go.Figure()\n",
        "fig.add_trace(go.Scatter(x=x, y=p_correct_given_dist, mode='lines', name='P(corrent | dist)'))\n",
        "\n",
        "# 添加布局和标题\n",
        "fig.update_layout(\n",
        "    title='P(Corrent | Dist)',\n",
        "    xaxis_title='Distance',\n",
        "    yaxis_title='Probability',\n",
        ")\n",
        "\n",
        "# 显示图表\n",
        "fig.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 542
        },
        "id": "zgVFFiFdytBa",
        "outputId": "e274c587-7008-458f-e1b6-5e49b7422bf6"
      },
      "execution_count": 73,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "<html>\n",
              "<head><meta charset=\"utf-8\" /></head>\n",
              "<body>\n",
              "    <div>            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_SVG\"></script><script type=\"text/javascript\">if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
              "        <script src=\"https://cdn.plot.ly/plotly-2.18.2.min.js\"></script>                <div id=\"39c40825-f7f3-4754-97a8-c71d12831e61\" class=\"plotly-graph-div\" style=\"height:525px; width:100%;\"></div>            <script type=\"text/javascript\">                                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById(\"39c40825-f7f3-4754-97a8-c71d12831e61\")) {                    Plotly.newPlot(                        \"39c40825-f7f3-4754-97a8-c71d12831e61\",                        [{\"mode\":\"lines\",\"name\":\"P(corrent | dist)\",\"x\":[0.0,0.008602993355857002,0.017205986711714003,0.025808980067571007,0.034411973423428006,0.043014966779285006,0.05161796013514201,0.06022095349099901,0.06882394684685601,0.07742694020271301,0.08602993355857001,0.09463292691442701,0.10323592027028403,0.11183891362614103,0.12044190698199803,0.129044900337855,0.13764789369371203,0.14625088704956904,0.15485388040542603,0.16345687376128304,0.17205986711714003,0.18066286047299704,0.18926585382885402,0.19786884718471104,0.20647184054056805,0.21507483389642504,0.22367782725228205,0.23228082060813904,0.24088381396399605,0.24948680731985304,0.25808980067571,0.26669279403156704,0.27529578738742405,0.28389878074328107,0.2925017740991381,0.30110476745499504,0.30970776081085205,0.31831075416670906,0.3269137475225661,0.33551674087842304,0.34411973423428005,0.35272272759013706,0.3613257209459941,0.3699287143018511,0.37853170765770805,0.38713470101356506,0.3957376943694221,0.4043406877252791,0.4129436810811361,0.42154667443699306,0.4301496677928501,0.4387526611487071,0.4473556545045641,0.45595864786042106,0.4645616412162781,0.4731646345721351,0.4817676279279921,0.4903706212838491,0.4989736146397061,0.5075766079955631,0.51617960135142,0.5247825947072771,0.5333855880631341,0.5419885814189911,0.5505915747748481,0.5591945681307051,0.5677975614865621,0.5764005548424191,0.5850035481982762,0.5936065415541331,0.6022095349099901,0.6108125282658471,0.6194155216217041,0.6280185149775611,0.6366215083334181,0.6452245016892751,0.6538274950451322,0.6624304884009892,0.6710334817568461,0.6796364751127031,0.6882394684685601,0.6968424618244171,0.7054454551802741,0.7140484485361311,0.7226514418919882,0.7312544352478452,0.7398574286037022,0.7484604219595591,0.7570634153154161,0.7656664086712731,0.7742694020271301,0.7828723953829871,0.7914753887388442,0.8000783820947012,0.8086813754505582,0.8172843688064152,0.8258873621622722,0.8344903555181291,0.8430933488739861,0.8516963422298431],\"y\":[0.9998604887828969,0.9998212272983028,0.9997714877785593,0.9997086381608632,0.9996294306428888,0.999529869748928,0.9994050552761872,0.9992489961619387,0.9990543909189792,0.9988123699324222,0.9985121946336479,0.9981409084204815,0.9976829342488495,0.9971196141768787,0.9964286869242597,0.9955837008802723,0.9945533621573741,0.9933008204936645,0.991782900352959,0.9899492907914501,0.9877417159127178,0.9850931183621738,0.9819269015869241,0.9781562926053967,0.9736839055797695,0.9684016068502335,0.962190802826419,0.9549232907981476,0.946462825658216,0.9366675577073286,0.9253934818905761,0.9124990000600349,0.8978506286715162,0.8813297804777642,0.8628404107851054,0.8423171546644589,0.819733408767028,0.7951086578444645,0.7685142471370383,0.7400767943317516,0.7099785474720145,0.6784542375448754,0.6457843282594106,0.6122849827012714,0.5782954763767006,0.5441641111810248,0.5102338611397443,0.4768289757672902,0.44424358789811863,0.412733063606769,0.38250845858174704,0.35373407807274465,0.32652783325675816,0.3009638793455367,0.2770769186625335,0.25486754354788765,0.2343080553525607,0.21534829908454692,0.1979211730397381,0.1819475895861485,0.16734076485966276,0.15400979556046396,0.14186253923969527,0.13080785245412713,0.12075726253207993,0.11162615748175997,0.10333457858487369,0.0958076946531739,0.0889760282627841,0.08277549430846019,0.07714730113554862,0.07203775504680036,0.06739800054052221,0.06318372137804901,0.05935482151382764,0.05587509997311087,0.05271192980703437,0.04983594815189834,0.047220762028261906,0.04484267270569065,0.04268042011557038,0.040714947818981106,0.038929188347374966,0.03730786826393332,0.035837331989441355,0.034505383255950796,0.0333011429613589,0.03221492217281915,0.031238109047208494,0.03036306848808168,0.02958305342980626,0.028892126722945145,0.0282850926847578,0.027757437470928403,0.02730527751661691,0.026925315384928743,0.026614802447905813,0.026371507908668348,0.02619369375328237,0.02608009529747255],\"type\":\"scatter\"}],                        {\"template\":{\"data\":{\"histogram2dcontour\":[{\"type\":\"histogram2dcontour\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"choropleth\":[{\"type\":\"choropleth\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"histogram2d\":[{\"type\":\"histogram2d\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"heatmap\":[{\"type\":\"heatmap\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"heatmapgl\":[{\"type\":\"heatmapgl\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"contourcarpet\":[{\"type\":\"contourcarpet\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"contour\":[{\"type\":\"contour\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"surface\":[{\"type\":\"surface\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"},\"colorscale\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]]}],\"mesh3d\":[{\"type\":\"mesh3d\",\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"parcoords\":[{\"type\":\"parcoords\",\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterpolargl\":[{\"type\":\"scatterpolargl\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"bar\":[{\"error_x\":{\"color\":\"#2a3f5f\"},\"error_y\":{\"color\":\"#2a3f5f\"},\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"scattergeo\":[{\"type\":\"scattergeo\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterpolar\":[{\"type\":\"scatterpolar\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"scattergl\":[{\"type\":\"scattergl\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatter3d\":[{\"type\":\"scatter3d\",\"line\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scattermapbox\":[{\"type\":\"scattermapbox\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scatterternary\":[{\"type\":\"scatterternary\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"scattercarpet\":[{\"type\":\"scattercarpet\",\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}}}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"baxis\":{\"endlinecolor\":\"#2a3f5f\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"#2a3f5f\"},\"type\":\"carpet\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"#EBF0F8\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"#C8D4E3\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}],\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"#E5ECF6\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}]},\"layout\":{\"autotypenumbers\":\"strict\",\"colorway\":[\"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#FFA15A\",\"#19d3f3\",\"#FF6692\",\"#B6E880\",\"#FF97FF\",\"#FECB52\"],\"font\":{\"color\":\"#2a3f5f\"},\"hovermode\":\"closest\",\"hoverlabel\":{\"align\":\"left\"},\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"#E5ECF6\",\"polar\":{\"bgcolor\":\"#E5ECF6\",\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"ternary\":{\"bgcolor\":\"#E5ECF6\",\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"},\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\"}},\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"ticks\":\"\"}},\"colorscale\":{\"sequential\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"sequentialminus\":[[0.0,\"#0d0887\"],[0.1111111111111111,\"#46039f\"],[0.2222222222222222,\"#7201a8\"],[0.3333333333333333,\"#9c179e\"],[0.4444444444444444,\"#bd3786\"],[0.5555555555555556,\"#d8576b\"],[0.6666666666666666,\"#ed7953\"],[0.7777777777777778,\"#fb9f3a\"],[0.8888888888888888,\"#fdca26\"],[1.0,\"#f0f921\"]],\"diverging\":[[0,\"#8e0152\"],[0.1,\"#c51b7d\"],[0.2,\"#de77ae\"],[0.3,\"#f1b6da\"],[0.4,\"#fde0ef\"],[0.5,\"#f7f7f7\"],[0.6,\"#e6f5d0\"],[0.7,\"#b8e186\"],[0.8,\"#7fbc41\"],[0.9,\"#4d9221\"],[1,\"#276419\"]]},\"xaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"automargin\":true,\"zerolinewidth\":2},\"yaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"ticks\":\"\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\",\"automargin\":true,\"zerolinewidth\":2},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\",\"gridwidth\":2},\"yaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\",\"gridwidth\":2},\"zaxis\":{\"backgroundcolor\":\"#E5ECF6\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showbackground\":true,\"ticks\":\"\",\"zerolinecolor\":\"white\",\"gridwidth\":2}},\"shapedefaults\":{\"line\":{\"color\":\"#2a3f5f\"}},\"annotationdefaults\":{\"arrowcolor\":\"#2a3f5f\",\"arrowhead\":0,\"arrowwidth\":1},\"geo\":{\"bgcolor\":\"white\",\"landcolor\":\"#E5ECF6\",\"subunitcolor\":\"white\",\"showland\":true,\"showlakes\":true,\"lakecolor\":\"white\"},\"title\":{\"x\":0.05},\"mapbox\":{\"style\":\"light\"}}},\"title\":{\"text\":\"P(Corrent | Dist)\"},\"xaxis\":{\"title\":{\"text\":\"Distance\"}},\"yaxis\":{\"title\":{\"text\":\"Probability\"}}},                        {\"responsive\": true}                    ).then(function(){\n",
              "                            \n",
              "var gd = document.getElementById('39c40825-f7f3-4754-97a8-c71d12831e61');\n",
              "var x = new MutationObserver(function (mutations, observer) {{\n",
              "        var display = window.getComputedStyle(gd).display;\n",
              "        if (!display || display === 'none') {{\n",
              "            console.log([gd, 'removed!']);\n",
              "            Plotly.purge(gd);\n",
              "            observer.disconnect();\n",
              "        }}\n",
              "}});\n",
              "\n",
              "// Listen for the removal of the full notebook cells\n",
              "var notebookContainer = gd.closest('#notebook-container');\n",
              "if (notebookContainer) {{\n",
              "    x.observe(notebookContainer, {childList: true});\n",
              "}}\n",
              "\n",
              "// Listen for the clearing of the current output cell\n",
              "var outputEl = gd.closest('.output');\n",
              "if (outputEl) {{\n",
              "    x.observe(outputEl, {childList: true});\n",
              "}}\n",
              "\n",
              "                        })                };                            </script>        </div>\n",
              "</body>\n",
              "</html>"
            ]
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "threshold_certain = 0.4\n",
        "threshold_doubt = 0.6"
      ],
      "metadata": {
        "id": "ZqGc4j_d0alk"
      },
      "execution_count": 99,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "feature_folder =  \"/content/Haruhi ep1/feature/\"\n",
        "save_name = \"/content/output_ep1.txt\""
      ],
      "metadata": {
        "id": "88OGD80-0z93"
      },
      "execution_count": 104,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "feature_folder =  \"/content/Haruhi ep10/feature/\"\n",
        "save_name = \"/content/output_ep10.txt\""
      ],
      "metadata": {
        "id": "-qA4W_GR4xYj"
      },
      "execution_count": 106,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# count how many files exist in feature_folder\n",
        "import os\n",
        "N_files = len( os.listdir(feature_folder) )\n",
        "N_files += 100"
      ],
      "metadata": {
        "id": "v0xncfCr0_HI"
      },
      "execution_count": 107,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "feature_folder =  \"/content/Haruhi ep1/feature/\"\n",
        "\n",
        "在feature_folder下，有形如 <id>_<台词>.wav.pkl的文件\n",
        "如:\n",
        "0_要到几岁才开始不相信圣诞老人的存在.wav.pkl\n",
        "\n",
        "将id从0到N_files循环，\n",
        "\n",
        "在feature_folder下寻找以id开头,'.wav.pkl'结尾的文件\n",
        "\n",
        "并且提取id的字符串打印\n",
        "\n",
        "使用python实现"
      ],
      "metadata": {
        "id": "Z0Uqe6e71Y4j"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "\n",
        "file_list = os.listdir(feature_folder)\n",
        "\n",
        "\n",
        "\n",
        "with open(save_name, \"w\", encoding=\"utf-8\") as f_out:\n",
        "\n",
        "    for id in range(N_files):\n",
        "\n",
        "        deal_file_name = '';\n",
        "\n",
        "        for file in file_list:\n",
        "            if file.startswith(str(id) + '_') and file.endswith('.wav.pkl'):\n",
        "                deal_file_name = file\n",
        "                id_str = file.split('_')[1]\n",
        "                break\n",
        "\n",
        "        if deal_file_name == '':\n",
        "            # print('file not found')\n",
        "            continue\n",
        "\n",
        "        full_file_name = os.path.join(feature_folder, deal_file_name)\n",
        "\n",
        "        with open(  full_file_name,'rb') as f:\n",
        "            feature = pickle.load(f)\n",
        "\n",
        "        predicted_label, distance = my_classifier.predict(feature)\n",
        "\n",
        "        role_name = ''\n",
        "\n",
        "        if distance < threshold_certain:\n",
        "            role_name = predicted_label\n",
        "        elif distance < threshold_doubt:\n",
        "            role_name = '(可能)' + predicted_label\n",
        "\n",
        "        output_str = role_name + ':「' +  id_str[:-8] + '」'\n",
        "\n",
        "        # print(output_str)\n",
        "        f_out.write(output_str + \"\\n\")\n",
        "\n",
        "    # break\n",
        ""
      ],
      "metadata": {
        "id": "zFG-fm9Q1Rt1"
      },
      "execution_count": 108,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "dVdoSIGd2orx"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}